package com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorder;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorderentry;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.IErpPurchaseorderService;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.IErpPurchaseorderentryService;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.dto.SaveErpPurchaserequsetFormDataDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.entity.ErpPurchaserequset;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.entity.ErpPurchaserequsetentry;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.mapper.ErpPurchaserequsetentryMapper;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.dto.ErpPurchaserequsetListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.mapper.ErpPurchaserequsetMapper;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.service.IErpPurchaserequsetService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-24
 */
@Service
@AllArgsConstructor
public class ErpPurchaserequsetServiceImpl extends BaseService<ErpPurchaserequsetMapper, ErpPurchaserequset> implements IErpPurchaserequsetService {

	private ErpPurchaserequsetentryMapper erpPurchaserequsetentryMapper;

	private final IErpPurchaseorderService erpPurchaseorderService;

	private final IErpPurchaseorderentryService erpPurchaseorderentryService;

	@Override
	public IPage<ErpPurchaserequset> getPageList(ErpPurchaserequsetListDto pageListDto) {
		Wrapper<ErpPurchaserequset> wrapper = Wrappers.<ErpPurchaserequset>query().lambda()
				.eq(!StringUtil.isEmpty(pageListDto.getF_DocunNumber()), ErpPurchaserequset::getFDocunnumber, pageListDto.getF_DocunNumber())
				.like(!StringUtil.isEmpty(pageListDto.getF_DocumentStatus()), ErpPurchaserequset::getFDocumentstatus, pageListDto.getF_DocumentStatus())
				.like(!StringUtil.isEmpty(pageListDto.getF_ChargePeople()), ErpPurchaserequset::getFChargepeople, pageListDto.getF_ChargePeople())
				.eq(!StringUtil.isEmpty(pageListDto.getF_DeliveryUnit()), ErpPurchaserequset::getFDeliveryunit, pageListDto.getF_DeliveryUnit())
				.eq(!StringUtil.isEmpty(pageListDto.getF_Emergencylevel()), ErpPurchaserequset::getFEmergencylevel, pageListDto.getF_Emergencylevel())
				.gt(pageListDto.getF_DocumentDate_Start() != null, ErpPurchaserequset::getFDocumentdate, pageListDto.getF_DocumentDate_Start())
				.lt(pageListDto.getF_DocumentDate_End() != null, ErpPurchaserequset::getFDocumentdate, pageListDto.getF_DocumentDate_End())
				.like(!StringUtil.isEmpty(pageListDto.getF_InvalidMark()), ErpPurchaserequset::getFInvalidmark, pageListDto.getF_InvalidMark());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpPurchaserequset(ErpPurchaserequset erpPurchaserequset, List<ErpPurchaserequsetentry> erpPurchaserequsetentryList) {
		String erpPurchaserequsetId = IdWorker.get32UUID();
		erpPurchaserequset.setFId(erpPurchaserequsetId);
		erpPurchaserequsetentryList.forEach(erpPurchaserequsetentry -> erpPurchaserequsetentry.setFRequsetid(erpPurchaserequsetId));
		this.saveBatch(erpPurchaserequsetentryList, ErpPurchaserequsetentry.class, ErpPurchaserequsetentryMapper.class);
		boolean isSuccess = this.save(erpPurchaserequset);
		return isSuccess ? erpPurchaserequsetId : null;
	}
	public String examine(String id,SaveErpPurchaserequsetFormDataDto formDto) {
		String erpPurchaseorderId = IdWorker.get32UUID();
		ErpPurchaseorder erpPurchaseorder = new ErpPurchaseorder();
		erpPurchaseorder.setFId(erpPurchaseorderId);
		erpPurchaseorder.setFPurchaseorder(formDto.getErpPurchaserequsetDto().getFDocunnumber());
		erpPurchaseorder.setFSourcenumber(formDto.getErpPurchaserequsetDto().getFDocunnumber());
		erpPurchaseorder.setFRequestid(id);
		erpPurchaseorder.setFPurchasepeople(formDto.getErpPurchaserequsetDto().getFChargepeople());
		erpPurchaseorder.setFDeliveryunit(formDto.getErpPurchaserequsetDto().getFDeliveryunit());
		erpPurchaseorder.setFPurchaseorder(formDto.getErpPurchaserequsetDto().getFDocunnumber());
		erpPurchaseorder.setFPurchasetype(formDto.getErpPurchaserequsetDto().getFApplicationpurposes());
		erpPurchaseorder.setFPurchaseorder(formDto.getErpPurchaserequsetDto().getFDocunnumber());
		erpPurchaseorder.setFDeliverydate(formDto.getErpPurchaserequsetDto().getFDeliverydate());
		erpPurchaseorder.setFTotalpurchases(new BigDecimal(formDto.getErpPurchaserequsetDto().getFActualamount()));
		erpPurchaseorder.setFDescription(formDto.getErpPurchaserequsetDto().getFDescription());
		erpPurchaseorder.setFCreatoruserid(formDto.getErpPurchaserequsetDto().getFCreatoruserid());
		erpPurchaseorder.setFPaymentmethod("1");
		erpPurchaseorder.setFAuditmark(1);
		erpPurchaseorder.setFInvalidmark(1);
		erpPurchaseorder.setFCreatortime(LocalDateTime.now());
		boolean isSuccess=erpPurchaseorderService.save(erpPurchaseorder);
		List<ErpPurchaseorderentry> erpPurchaseorderentryList=new ArrayList<ErpPurchaseorderentry>();
		for (int i=0;i<formDto.getErpPurchaserequsetentryDto().size();i++)
		{
			ErpPurchaseorderentry erpPurchaseorderentry=new ErpPurchaseorderentry();
			erpPurchaseorderentry.setFGoodsid(formDto.getErpPurchaserequsetentryDto().get(i).getFGoodsid());
			erpPurchaseorderentry.setFGoodsname(formDto.getErpPurchaserequsetentryDto().get(i).getFGoodsname());
			erpPurchaseorderentry.setFOrderid(erpPurchaseorder.getFId());
			erpPurchaseorderentry.setFGoodcode(formDto.getErpPurchaserequsetentryDto().get(i).getFGoodcode());
			erpPurchaseorderentry.setFSpecs(formDto.getErpPurchaserequsetentryDto().get(i).getFSpecs());
			erpPurchaseorderentry.setFUnitid(formDto.getErpPurchaserequsetentryDto().get(i).getFUnitid());
			erpPurchaseorderentry.setFUnit(formDto.getErpPurchaserequsetentryDto().get(i).getFUnit());
			erpPurchaseorderentry.setFQuantity(formDto.getErpPurchaserequsetentryDto().get(i).getFQuantity());
			erpPurchaseorderentry.setFAmount(formDto.getErpPurchaserequsetentryDto().get(i).getFAmount());
			erpPurchaseorderentry.setFUnitprice(formDto.getErpPurchaserequsetentryDto().get(i).getFUnitprice());
			erpPurchaseorderentry.setFDes(formDto.getErpPurchaserequsetentryDto().get(i).getFDes());
			erpPurchaseorderentry.setFReceiptQua(0);//已入库数量默认是0
			erpPurchaseorderentry.setFNoReceiptQua(formDto.getErpPurchaserequsetentryDto().get(i).getFQuantity());//未入库数量
			erpPurchaseorderentryList.add(erpPurchaseorderentry);
		}
		erpPurchaseorderentryService.saveBatch(erpPurchaseorderentryList);
		ErpPurchaserequset erpPurchaserequset=new ErpPurchaserequset();
		erpPurchaserequset.setFId(id);
		erpPurchaserequset.setFAuditmark("2");
		erpPurchaserequset.setFDocumentstatus("2");
		this.updateById(erpPurchaserequset);
		return isSuccess ? erpPurchaseorderId : null;
	}


//	@Override
//	public String examineErpPurchaserequset(String Id) {
//		return "1";
//	}

	@Override
	public boolean updateErpPurchaserequset(String id, ErpPurchaserequset erpPurchaserequset, List<ErpPurchaserequsetentry> erpPurchaserequsetentryList) {
		erpPurchaserequset.setFId(id);
		erpPurchaserequsetentryList.forEach(erpPurchaserequsetentry -> erpPurchaserequsetentry.setFRequsetid(id));
		erpPurchaserequsetentryMapper.delete(Wrappers.<ErpPurchaserequsetentry>query().lambda().eq(ErpPurchaserequsetentry::getFRequsetid, id));
		this.saveBatch(erpPurchaserequsetentryList, ErpPurchaserequsetentry.class, ErpPurchaserequsetentryMapper.class);
		return this.updateById(erpPurchaserequset);
	}
	public List<ErpPurchaserequsetentry> getErpPurchaserequsetentryByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpPurchaserequsetentry>query().lambda().eq(ErpPurchaserequsetentry::getFRequsetid, parentId);
		return erpPurchaserequsetentryMapper.selectList(wrapper);
	}
}
